<?php

/**
 * 后台基础控制器
 * 
 * @author 陈蕾
 */

abstract class Controller_Admin_Base extends Controller {

    public $content;
    protected $_auto = false;

    public function before()
    {
        session_cache_limiter('private, must-revalidate');
    	// 未登录则跳转至登录页
        if ( ! Auth::has_logged_in()) {
            $this->request->redirect('admin/login');
        }
        $this->content = "admin/{$this->request->controller}/{$this->request->action}";
    }

    // 预设模板
    public function prepare($title, $content = NULL)
    {
        if ($content) {
            $this->content = $content;
        }

        $this->template = View::factory('admin/panel/inner');
        $this->template->bind_global('title', $title);

        $this->content = View::factory($this->content);
    }

    // 提示消息
    public function message($text, $forward = NULL, $seconds = 3)
    {
        if (is_array($text)) {
            $text = array_shift($text);
        }

        if (empty($forward)) {
            $forward = 'javascript:window.history.back();';
            $forward_text = '点击这里返回上一页';
        } else {
            if (strpos($forward, '://') === FALSE) {
                $forward = URL::site("admin/{$forward}");
            }
            $forward_text = '点击这里继续操作';
        }

        $this->prepare('系统提示', 'admin/message');
        $this->content->message = array('text' => $text, 'forward' => $forward, 'forward_text' => $forward_text, 'seconds' => $seconds);
        $this->request->response = $this->template;

        $this->render();
    }

    // 输出模板
    public function render()
    {
        if ($this->template) {
            $this->request->response = $this->template;
            $this->template->content = $this->content;
        }
    }

}